﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ScriptRunner
{
    public static class CommandLineParser
    {
        public static bool TryParseCommandLine(string[] args,out string uniqueId,out string userName,out string password, out string ipcPortName, out string ipcObjectUri)
        {
            ipcPortName = null;
            ipcObjectUri = null;
            uniqueId = string.Empty;
            userName = string.Empty;
            password = string.Empty;
            foreach (string arg in args)
            {
                if (StartsWith(arg, "-uniqueId="))
                    uniqueId = GetArgument(arg);
                else if (StartsWith(arg, "-userName="))
                    userName = GetArgument(arg);
                else if (StartsWith(arg, "-password="))
                    password = GetArgument(arg);
                else if (StartsWith(arg, "-ipcPortName="))
                      ipcPortName = GetArgument(arg);
                else if (StartsWith(arg, "-ipcObjectUri="))
                      ipcObjectUri = GetArgument(arg);
            }
            return false;
        }

        private static bool StartsWith(string arg, string str)
        {
            return arg.StartsWith(str, StringComparison.OrdinalIgnoreCase);
        }

        private static string GetArgument(string arg)
        {
            int idx = arg.IndexOf("=");
            return idx >= 0 ? arg.Substring(idx + 1) : arg;
        }

        private static string[] GetArrayArgument(string arg) => GetArgument(arg).Split(',');
    }
}
